/**
 * 为方便自定义加载出分页的数据，将分页的加载单独封装
 * @author liweishuai
 */
jQuery.fn.extend({
    /**
     * param参数介绍{
     *     url:查询地址(必需)
     *     loadDataItem:单条数据的渲染(必需)，回传后台返回的数组元素
     *     ajaxParams:查询参数(非必需)
     *     onloadFinish:全部渲染完成以后执行的回调(非必需)
     *     pageSize:每页的条数(非必需)
     *     pageList:每页条数选择列表(非必需)
     * }
     * 后台返回模板{total:0,rows:[]}
     * @param param
     */
    paginationInit: function (param) {
        var _$pageContainer = this;
        //声明闭包对象
        var paginationComponent = (function () {
            var meObj;
            var defaultParam = {
                pageNum: 1,//第几页
                pageSize: 20,//每页的条数
                ajaxParams: {},
                pageList: [20,45,100,500],
                loadDataItem: function (itemData) {
                },//
                onloadFinish: function () {
                }
            };
            var curPage = 1, totalPage = 0, dataTotal = 0;
            var _$paninationInfo = $('<span class="pagination-info"></span>');
            var _$paginationOutline = $('<ul class="pagination pagination-outline"></ul>');
            var _pagePre = '<li class="page-pre"><a href="javascript:void(0)">‹</a></li>';
            var _pageNext = '<li class="page-next"><a href="javascript:void(0)">›</a></li>';
            var _$pageList = $('<span class="page-list">' +
                '<span class="btn-group dropup">' +
                '<button type="button" class="btn btn-default btn-outline dropdown-toggle" data-toggle="dropdown" aria-expanded="false">' +
                '<span class="page-size"></span><span class="caret"></span>' +
                '</button>' +
                '<ul class="dropdown-menu" ></ul>' +
                '</span> 条记录每页</span>');
            var _noDataShow = '<div class="fixed-table-pagination nodata">暂无数据</div>';
            return {
                query: function (param) {
                    meObj = this;
                    defaultParam = $.extend(defaultParam, param);
                    curPage = defaultParam.pageNum;
                    var pageParams = {
                        pageNum: defaultParam.pageNum,
                        pageSize: defaultParam.pageSize
                    };
                    $.ajax({
                        type: "post",
                        url: param.url,
                        data: $.extend(pageParams, param.ajaxParams),
                        dataType: "json",
                        success: function (data) {
                            dataTotal = data.total;
                            _$pageContainer.empty();
                            //容器中装载数据
                            for (var i = 0; i < data.rows.length; i++) {
                                _$pageContainer.append(defaultParam.loadDataItem(data.rows[i]));
                            }
                            if (dataTotal == 0) {
                                _$pageContainer.html(_noDataShow);
                            }
                            meObj.buildPageLeft();//加载左边部分
                            meObj.buildPageRight();//加载右边部分
                            meObj.domAppend();//容器中装载分页组件
                            meObj.bindClick();//分页组件绑定点击事件
                            defaultParam.onloadFinish();//装载完毕执行回调
                        }
                    });
                },
                getPrePageNum: function () {
                    if (curPage == 1) {
                        return totalPage;
                    }
                    return curPage - 1;
                },
                getNextPageNum: function () {
                    if (curPage == totalPage) {
                        return 1;
                    }
                    return curPage + 1;
                },
                buildPageLeft: function () {
                    if (dataTotal % defaultParam.pageSize == 0) {
                        totalPage = dataTotal / defaultParam.pageSize;
                    } else {
                        totalPage = parseInt(dataTotal / defaultParam.pageSize) + 1;
                    }
                    totalPage = totalPage == 0 ? 1 : totalPage;
                    _$paninationInfo.empty().append('共' + dataTotal + '条记录，共' + totalPage + '页，当前第' + curPage + '页。');

                    _$pageList.find(".page-size").html(defaultParam.pageSize);
                    _$pageList.find(".dropdown-menu").empty();
                    for (var i = 0; i < defaultParam.pageList.length; i++) {
                        if (dataTotal < defaultParam.pageList[i]) {
                            continue;
                        }
                        var isActive = defaultParam.pageSize == defaultParam.pageList[i] ? "active" : "";
                        _$pageList.find(".dropdown-menu").append('<li class="' + isActive + '"><a href="javascript:void(0)">' + defaultParam.pageList[i] + '</a></li>')
                    }
                    if (_$pageList.find(".dropdown-menu li").length == 0) {
                        _$pageList.hide();
                    } else {
                        _$pageList.show();
                    }
                },
                buildPageRight: function () {
                    var pageArr = new Array();
                    //总页数大于1时才显示上下页按钮
                    if (totalPage > 1) {
                        for (var i = 1; i <= totalPage; i++) {
                            if (i == curPage) {
                                pageArr[i - 1] = '<li class="page-number active"><a href="javascript:void(0)">' + i + '</a></li>';
                            } else {
                                pageArr[i - 1] = '<li class="page-number"><a href="javascript:void(0)">' + i + '</a></li>';
                            }
                        }
                        if (totalPage > 5) {
                            if (curPage - 1 > 3) {
                                pageArr[1] = '<li class="page-last-separator disabled"><a href="javascript:void(0)">...</a></li>';
                                for (var i = 2; i < curPage - 4; i++) {
                                    pageArr[i] = '';
                                }
                            }
                            if (curPage + 2 < totalPage) {
                                for (var i = curPage + 2; i < totalPage - 2; i++) {
                                    pageArr[i] = '';
                                }
                                pageArr[totalPage - 2] = '<li class="page-last-separator disabled"><a href="javascript:void(0)">...</a></li>';
                            }
                        }
                        pageArr.unshift(_pagePre);//上一页
                        pageArr.push(_pageNext);//下一页
                    }
                    _$paginationOutline.empty().append(pageArr.join(""));
                },
                domAppend: function () {
                    var allDom = '<div class="fixed-table-pagination" style="display: block;">' +
                        '<div class="pull-left pagination-detail">' +
                        _$paninationInfo[0].outerHTML +
                        _$pageList[0].outerHTML +
                        '</div>' +
                        '<div class="pull-right pagination">' + _$paginationOutline[0].outerHTML +
                        '</div>'
                    '</div>';
                    _$pageContainer.append(allDom);
                },
                bindClick: function () {
                    _$pageContainer.find("li.page-number:not(.active)").each(function () {
                        var num = $(this).find("a").html();
                        $(this).click(function () {
                            meObj.query($.extend(defaultParam, {pageNum: num}));
                        });
                    });
                    _$pageContainer.find(".page-pre").click(function () {
                        meObj.query($.extend(defaultParam, {pageNum: meObj.getPrePageNum()}));
                    });
                    _$pageContainer.find(".page-next").click(function () {
                        meObj.query($.extend(defaultParam, {pageNum: meObj.getNextPageNum()}));
                    });
                    _$pageContainer.find(".dropdown-menu li:not(.active)").click(function () {
                        meObj.query($.extend(defaultParam, {
                            pageNum: 1,
                            pageSize: $(this).find("a").text()
                        }));
                    });
                }
            };
        })();
        paginationComponent.query(param);
    }
});